let urlEntryHash = null

class URLEntry {
    constructor() {
    }

    public response(param, extData?) {
        if (param.view) {
            //TODO:跳转到view
        }
        if (param.popup) {
            //TODO:打开popop
        }
    }

    public checkEnabled(param, extData?) {
        return true
    }
}

// urlEntryHash["view://task"] = "view://task&view=main&popup=task"

let addEntry = (key) => {
    if (urlEntryHash[key] != null) {
        return
    }
    urlEntryHash[key] = new URLEntry()
    return urlEntryHash[key]
}


let getURLEntries = () => {
    if (urlEntryHash != null)
        return urlEntryHash
    urlEntryHash = {}
    let entry = null

    //任务跳转
    entry = addEntry("view://task")
    entry.checkEnabled = (param, extData?) => {
        let isOpen = false
        return isOpen
    }
    //todo...




    return urlEntryHash
}

export class URLContext {
    private static created: boolean = false;
    constructor() {
        if (URLContext.created) {
            throw new Error("URLContext 是单例");
        }
        URLContext.created = true;
    }

    public responseURL(url, extData?) {
        //解析url
        let data = this.resolveUrl(url)
        let entry = getURLEntries()[data["key"]]
        if (entry == null)
            return
        //判断是否可以跳转
        if (!entry.checkEnabled()) {
            //TODO:show tips
            return
        }
        //跳转
        entry.response(data, extData)
    }

    public resolveUrl(str) {
        let info = str.split("&")
        let data = {}
        data["key"] = info[0]
        for (let index = 1; index < info.length; index++) {
            let pair = info[index].split("=")
            data[pair[0]] = pair[1]
        }
        return data
    }
}

